import { wxPayment } from '@/core/app'
export default {
    data() {
        return {
        }
    },
    methods: {
        //签署合同
         sign(id){
            const app = this
            // uni.showModal({
            //     title: '签署方式',
            //     content: '选择您的签署方式',
            //     // showCancel: false,
            //     confirmText: "线下签署",
            //     cancelText: "在线签署",
            //     success: res => {
            //         if (res.confirm) {
            //             app.$navTo('page_work/offlinePlay/index',{orderId:id,isPay:false,actualPrice:0})
            //         }
            //         if (res.cancel) {
                        app.onlineSign(id)
            //         }
            //     }
            // })
        },
        //实名认证
        async onlineSign(id){
            const authStatus = await this.getAuthStatus();
            if(authStatus){
                id && this.createSignTask(id);
            }else{
                this.authUrl();
            }
        },

        //当前用户认证状态
        getAuthStatus(){
            return new Promise((resolve, reject) => {
                this.$request.get('wx/fdd/getAuthStatus').then(r=>{
                    if(r.data){
                        const identStatus = r.data.identStatus;
                        if(identStatus == 'identified'){
                            resolve(true)
                        }
                    }
                    resolve(false)
                }).catch(err=>{
                    this.$toast(err.errmsg)
                    reject(false)
                })
            })
        },
        //获取授权链接
        authUrl() {
            this.$request.get('wx/fdd/authUrl').then(r=>{
                const url = '/pagesFace/pages/webview/webview?url=' + encodeURIComponent(r.data.authUrl)
                uni.navigateTo({ url })
            }).catch(err=>{
                this.$toast(err.errmsg)
            })
        },
        //继续签署
        getSignUrl(id){
            this.$request.post('wx/fdd/getSignUrl',{orderId:id}).then(r=>{
                const url = '/pagesFace/pages/webview/webview?url=' + encodeURIComponent(r.data.actorSignTaskEmbedUrl)
                uni.navigateTo({ url })
            }).catch(err=>{
                this.$toast(err.errmsg)
            })
        },
        //创建签署任务
        createSignTask(id){
            const params = {
                orderId:id,
                redirectUrl:"pages/index/index"
            }
            this.$request.post('wx/fdd/createSignTask',params).then(r=>{
                const url = '/pagesFace/pages/webview/webview?url=' + encodeURIComponent(r.data.actorSignTaskEmbedUrl)
                uni.navigateTo({ url })
            }).catch(err=>{
                this.$toast(err.errmsg)
            })
        },
        //支付
        prepay(orderId,actualPrice){
            const app = this
            uni.showModal({
                title: '支付方式',
                content: '选择您的支付方式',
                // showCancel: false,
                confirmText: "线下支付",
                cancelText: "微信支付",
                success: res => {
                    if (res.confirm) {
                        app.$navTo('page_work/offlinePlay/index',{orderId:orderId,isPay:true,actualPrice:actualPrice})
                    }
                    if (res.cancel) {
                        // app.popup = true
                        const params = {
                            orderId:orderId
                        }
                        app.$request.post('wx/order/prepay',params).then(r=>{
                            app.onSubmitCallback(r,orderId)
                        }).catch(err=>{
                            app.$toast(err.errmsg)
                        })
                    }
                }
            })
        },

        // 订单提交成功后回调
        onSubmitCallback(result,orderId) {
            const app = this
            // 发起微信支付
            wxPayment(result.data)
                .then(() => {
                    app.$navTo('page_work/checkout/playResult',{orderTotalPrice:app.order.orderTotalPrice,orderId:orderId})
                    // app.$success('支付成功')
                    app.getList()
                })
                .catch(err => {
                    console.log(err)
                    app.$error('订单未支付')
                })
                .finally(() => {
                    app.disabled = false
                    // app.getList()
                })
        },
        //确认收货
        confirm(id){
            const app = this
            uni.showModal({
                title: "提示",
                content: "确认收货吗?",
                success({ confirm }) {
                    confirm && app.onConfirm(id)
                }
            });
        },
        //确认收货请求
        onConfirm(id,fn){
            if(this.disabledBtn){
                return;
            }
            this.disabledBtn = true;
            this.$request.post('wx/order/confirm', {orderId:id}).then(({data}) => {
                this.$toast('确认收货成功')
                fn && fn()
            }).catch(err=>{
                this.$toast(err.errmsg)
            }).finally(()=>{
                this.disabledBtn = false;
            })
        },
    }
}